﻿/**
<code>
  <author name="Zealic" email="zealic(at)gmail.com" type="Creator"/>
</code>
**/
using System;


namespace Zealib.Transform
{
    /// <summary>
    /// Transformer base define.
    /// </summary>
    public abstract class AbstractTransformer : ITransformer
    {
        #region ITransformer Members
        public abstract bool CanTransformTo<TDestiation>();

        public abstract bool CanTransformBack<TSource>();

        public virtual TDestiation TransformTo<TDestiation>(object source)
        {
            throw GetTransformToException(source, typeof (TDestiation));
        }

        public virtual TSource TransformBack<TSource>(object destiation)
        {
            throw GetTransformBackException(destiation, typeof(TSource));
        }

        #endregion

        protected virtual Exception GetTransformToException(object source, Type destinationType)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (destinationType == null) throw new ArgumentNullException("destinationType");
            string myName = GetType().Name;
            string fullName = source.GetType().FullName;
            string destName = destinationType.FullName;
            string msg = string.Format("'{0}' is unable to transform '{1}' to '{2}'.",
                myName, fullName, destName);
            return new NotSupportedException(msg);
        }

        protected virtual Exception GetTransformBackException(object destination,Type sourceType)
        {
            if (destination == null) throw new ArgumentNullException("destination");
            string myName = GetType().Name;
            string fullName = destination.GetType().FullName;
            string srcName = sourceType.FullName;
            string msg = string.Format("'{0}' is unable transform '{1}' back '{2}'.",
                myName, fullName, srcName);
            return new NotSupportedException(msg);
        }

    }
}
